package org.mao.book;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.collect.Lists;

import net.sf.json.JSONObject;

public class ReadBook {

	private String protocol = "http";

	private String domain = "bbs.tianya.cn";

	private String port = "80";

	private Integer minPage = 100 * 0 + 10 * 0 + 1;

	private Integer maxPage = minPage;
	
	private File file;
	
	private String keyword="<div class=\"bbs-content\">";

	public static void main(String[] arg) {
		ReadBook readBook = new ReadBook();
		readBook.read();
	}

	public void read(){
		try {
			file = new File("D:\\春秋.txt");
			for(int i=minPage;i<=maxPage;i++){
				System.out.println("第"+i+"页");
				String html = request("post-no05-381555-"+i+".shtml");//#ty_vip_look[%E6%B5%B7%E6%A3%A0%E6%A0%96%E9%9C%B2]
				System.out.println(html);
				//System.out.println(html.substring(html.indexOf("前面的情节是")-30, html.indexOf("前面的情节是")+30));
				//find2(0, html);
			}
			//requestResource("http://static.tianyaui.com/img/static/2011/imgloading.gif", "gif");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public void find2(int start, String content) throws Exception{
		int startPosition = content.indexOf(keyword, start);
		if (startPosition > 0) {
			int endPosition = print2(content, startPosition);
			find2(endPosition, content);
		}
	}
	
	public int print2(String content, int startPosition) throws Exception{
		int endPosition = content.indexOf("</div>", startPosition);
		content = content.substring(startPosition+keyword.length(), endPosition);
		content = content.replace("<br>", "\r\n");
		if(isContent(content)){
			System.out.println(content);
//			FileWriter fw = new FileWriter(file, true);
//			fw.write(content);
//			fw.close();
		}
		return endPosition;
	}
	
	public boolean isContent(String content){
		int position = filterWork(content);
		if(position!=-1){
			String startBlank = content.substring(0, position);
			startBlank = startBlank.trim();
			startBlank=startBlank.replace("\\s", "");
			startBlank=startBlank.replace("\t", "");
			startBlank=startBlank.replace("\r", "");
			startBlank=startBlank.replace("\n", "");
			startBlank=startBlank.replace("　", "");
			if(startBlank.length()<100){
				return true;
			}
		}
		return false;
	}
	
	public int filterWork(String content){
		int position = -1;
		List<String> list=Lists.newArrayList();
		list.add("第");
		list.add("序");
		list.add("引言");
		list.add("前言");
		list.add("前面的情节是");
		list.add("一、");
		list.add("二、");
		list.add("三、");
		list.add("四、");
		list.add("五、");
		list.add("六、");
		list.add("七、");
		list.add("八、");
		list.add("九、");
		list.add("十、");
		list.add("十一、");
		list.add("十二、");
		list.add("十三、");
		list.add("十四、");
		list.add("十五、");
		list.add("十六、");
		list.add("十七、");
		list.add("十八、");
		list.add("十九、");
		list.add("二一、");
		list.add("假虞灭虢");
		list.add("不定期");
		list.add("分道扬镳");
		list.add("海棠栖露");
		for(int i=0;i<list.size();i++){
			if(content.indexOf(list.get(i))!=-1){
				if(position==-1){
					position = content.indexOf(list.get(i));
				}else{
					if(content.indexOf(list.get(i))<position){
						position=content.indexOf(list.get(i));
					}
				}
			}
		}
		return position;
	}

	public String request(String module) throws Exception {
		System.out.println("开始请求接口-------------------------------");
		String url = new StringBuilder(150).append(protocol).append("://").append(domain).append(":").append(port).append("/").append(module).toString();
		System.out.println("接口地址: " + url);
		URL requestUrl = new URL(url);
		HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
		this.generatorConnection(connection);
		connection.connect();

		//PrintWriter out = new PrintWriter(connection.getOutputStream());
//		JSONObject requestJson = new JSONObject();
//		String requestData = requestJson.toString();
//		requestData = requestJson.toString();
//
//		System.out.println("接口参数: " + requestData);
//		out.print(requestData);
		//out.flush();
		//out.close();

		// 获取所有响应头字段
		Map<String, List<String>> map = connection.getHeaderFields();
		// 遍历所有的响应头字段
		 for (String attr : map.keySet()) {
		 System.out.println(attr + "--->" + map.get(attr));
		 }

		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		StringBuilder sb = new StringBuilder();
		String temp = null;
		while ((temp = br.readLine()) != null) {
			sb.append(temp);
		}
		// System.out.println(sb);
		return sb.toString();
	}
	
	public void requestResource(String url,String suffix) throws Exception {
		System.out.println("开始请求资源-------------------------------");
		//String url = new StringBuilder(150).append(protocol).append("://").append(domain).append(":").append(port).append("/").append(module).toString();
		System.out.println("资源地址: " + url);
		URL requestUrl = new URL(url);
		HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
		this.generatorResourceConnection(connection);

		PrintWriter out = new PrintWriter(connection.getOutputStream());
//		JSONObject requestJson = new JSONObject();
//		String requestData = requestJson.toString();
//		requestData = requestJson.toString();
//
//		System.out.println("接口参数: " + requestData);
//		out.print(requestData);
		out.flush();
		out.close();

		// 获取所有响应头字段
		Map<String, List<String>> map = connection.getHeaderFields();
		// 遍历所有的响应头字段
		for (String attr : map.keySet()) {
			System.out.println(attr + "--->" + map.get(attr));
		}
		
		File tempFile=new File("D:/temp."+suffix);
		FileOutputStream fos = new FileOutputStream(tempFile);
		BufferedInputStream bis=new BufferedInputStream(connection.getInputStream());
		byte[] b= new byte[1024];
		while((bis.read(b))!=-1){
			fos.write(b);
		}
		fos.flush();
		fos.close();
		bis.close();
	}

	public void generatorConnection(HttpURLConnection connection) throws Exception {
		// 设置通用的请求属性
		connection.setRequestMethod("GET");
		connection.setRequestProperty("accept", "text/html");
		connection.setRequestProperty("Source-Type", "1");
		connection.setRequestProperty("Content-Type", "text/html;charset=UTF-8");
		connection.setRequestProperty("connection", "Keep-Alive");
		connection.setRequestProperty("user-agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		connection.setDoOutput(true);
		connection.setDoInput(true);
	}

	public void generatorResourceConnection(HttpURLConnection connection) throws Exception {
		// 设置通用的请求属性
		connection.setRequestMethod("GET");
		connection.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
//		connection.setRequestProperty("Source-Type", "1");
//		connection.setRequestProperty("Content-Type", "text/html;charset=UTF-8");
//		connection.setRequestProperty("connection", "Keep-Alive");
//		connection.setRequestProperty("user-agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
		connection.setDoOutput(true);
		connection.setDoInput(true);
	}

}
